package com.douhuayuedu.douhua.wxapi;

import android.content.Intent;
import android.os.Bundle;

import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.event.EventBusPayResult;
import com.taptech.doufu.net.retrofit.RxBus;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.util.NotifyUtil;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends DiaobaoBaseActivity implements IWXAPIEventHandler {


    private IWXAPI wxAPI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerShareServices();
    }

    private void registerShareServices() {
        // wx
        wxAPI = WXAPIFactory.createWXAPI(/*getThisActivity()*/this, Constant.WX_APP_ID, true);
        wxAPI.registerApp(Constant.WX_APP_ID);
        wxAPI.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        wxAPI.handleIntent(intent, this);
    }


    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        if(baseResp.getType()== ConstantsAPI.COMMAND_PAY_BY_WX){
            EventBusPayResult eventBusWXPay = new EventBusPayResult();
            eventBusWXPay.setTag(EventBusPayResult.tag);
            if (baseResp.errCode == BaseResp.ErrCode.ERR_OK) {
                NotifyUtil.successPay();
                eventBusWXPay.setSuccess(true);
                eventBusWXPay.setMsg("充值成功");
            } else {
                eventBusWXPay.setSuccess(false);
                eventBusWXPay.setMsg(baseResp.errStr);
                NotifyUtil.errorPay(baseResp.errStr);
            }
            RxBus.getDefault().post(eventBusWXPay);
        }
        finish();
    }
}
